package com.hoyo.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.hoyo.common.VerifyCode;

public class CommonUtils {

	private static final String[] charSet = { "0", "1", "2", "3", "4", "5", "6", "7", "9", "a", "b", "c", "d", "e", "f",
			"g", "h", "i", "j", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
			"y", "z" };

	private static final int codeQty = 6;
	private static final int nameNum = 8;

	public static String generateVerifyCode() {
		StringBuilder builder = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < codeQty; i++) {
			builder.append(random.nextInt(10));
		}
		return builder.toString();
	}

	public static String generateUserName() {
		StringBuilder builder = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < nameNum; i++) {
			builder.append(charSet[random.nextInt(36)]);
		}
		return builder.toString().toUpperCase();
	}

	/**
	 * 生成随机字符串
	 * 
	 * @return
	 */
	public static String generateRandomName() {

		StringBuilder builder = new StringBuilder();
		Random random = new Random();
		int charNum = random.nextInt(8) + 8;
		for (int i = 0; i < charNum; i++) {
			builder.append(charSet[random.nextInt(36)]);
		}

		return builder.toString();
	}

	/**
	 * 校验结果
	 * 
	 * @param result
	 * @return
	 */
	public static Map<String, String> resultValidate(BindingResult result) {

		Map<String, String> errors = new HashMap<>();
		if (result.hasErrors()) {
			List<FieldError> fieldErrors = result.getFieldErrors();
			for (FieldError fieldError : fieldErrors) {
				errors.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
		}

		return errors;
	}

	/**
	 * 检测点是否在多边形内
	 * 
	 * @param nvert
	 *            节点数
	 * @param vertx
	 *            多边形x坐标集
	 * @param verty
	 *            多边形y坐标集
	 * @param testx
	 *            被监测点x坐标
	 * @param testy
	 *            被检测点y坐标
	 * @return 是否在多边形内
	 */
	public static boolean pnpoly(int nvert, BigDecimal[] vertx, BigDecimal[] verty, BigDecimal testx,
			BigDecimal testy) {

		BigDecimal maxX = vertx[0], minX = vertx[0], maxY = verty[0], minY = verty[0];
		for (int i = 1; i < nvert; i++) {

			if (maxX.subtract(vertx[i]).floatValue() < 0)
				maxX = vertx[i];
			if (minX.subtract(vertx[i]).floatValue() > 0)
				minX = vertx[i];
			if (maxY.subtract(verty[i]).floatValue() < 0)
				maxY = verty[i];
			if (minY.subtract(verty[i]).floatValue() > 0)
				minY = verty[i];
		}

		if (testx.subtract(minX).floatValue() < 0 || testx.subtract(maxX).floatValue() > 0
				|| testy.subtract(minY).floatValue() < 0 || testy.subtract(maxY).floatValue() > 0) {
			return false;
		}

		int i, j;
		boolean c = false;
		for (i = 0, j = nvert - 1; i < nvert; j = i++) {

			BigDecimal arg1 = vertx[j].subtract(vertx[i]);
			BigDecimal arg2 = testy.subtract(verty[i]);
			BigDecimal arg3 = verty[j].subtract(verty[i]);

			if (((verty[i].subtract(testy).floatValue() > 0) != (verty[j].subtract(testy).floatValue() > 0))
					&& (testx.subtract(arg1.multiply(arg2).divide(arg3, 10, BigDecimal.ROUND_HALF_EVEN).add(vertx[i]))
							.floatValue() < 0))
				c = !c;

			/*
			 * if( (verty[i].subtract(testy).floatValue()>0) !=
			 * (verty[j].subtract(testy).floatValue()>0) ){ BigDecimal arg4
			 * =arg1.multiply(arg2); BigDecimal arg5
			 * =arg4.divide(arg3,10,BigDecimal.ROUND_HALF_EVEN); BigDecimal arg6
			 * =arg5.add(vertx[i]); if(testx.subtract(arg6).floatValue() <0){ c
			 * = !c; } }
			 */

		}

		return c;
	}

	/**
	 * 检测验证码是否有效
	 * 
	 * @param code
	 * @param verifyCode
	 * @return
	 */
	public static boolean checkCodeAvailable(String code, VerifyCode verifyCode) {

		if (!code.equals(verifyCode.getCode()) || !verifyCode.isEnable()) {
			return false;
		}
		long expired = verifyCode.getExpired();
		long generateTime = verifyCode.getGenerateTime();
		if (System.currentTimeMillis() - generateTime > expired) {
			return false;
		}
		return true;
	}

	public static String readInput(BufferedReader br) throws IOException {

		try {
			StringBuilder sb = new StringBuilder();
			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				br.close();
			}
		}
		return "read request text failed";
	}
	
	public static void main(String[] args) {
		System.out.println(CommonUtils.generateRandomName());
	}
}
